package com.myproject.zhujiemian;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.itheima.tencentqq52.R;
import com.myproject.biji.bijiMainActivity;
import com.myproject.database.MYdatabase;
import com.myproject.database.MyAdapter;
import com.myproject.database.Time;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class TongjiPage extends Activity {
	
	private SQLiteDatabase timeReader;
	private Time selecttime;
	private Cursor cursor;
	private ListView list_tongjilist;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tongjipage);
		
		list_tongjilist = (ListView) findViewById(R.id.list_tongjilist);
		selecttime = new Time(this);
		timeReader = selecttime.getReadableDatabase();
	}

	// 跳转到坚持页面
	public void ToJianchiPage(View view) {
		Intent intent = new Intent(this, JianchiPage.class);
		intent.setAction("com.myproject.JianchiPage");
		startActivity(intent);
	}

	// 跳转到任务页面
	public void ToRenwuPage(View view) {
		Intent intent = new Intent(this, MainActivity.class);
		intent.setAction("android.intent.action.MAIN");
		startActivity(intent);
	}
	
	//查找数据库
	public void selecttime() {
		cursor = timeReader.query(Time.TABLE_NAME, null, null, null, null,
				null, null);
		TimeAdapter timeadapter = new TimeAdapter(this, cursor);
		list_tongjilist.setAdapter(timeadapter);
	}
	
	
	
	@Override
	protected void onResume() {
		super.onResume();
		selecttime();
	}
	
}


class TimeAdapter extends BaseAdapter {

	private Context context;
	private Cursor cursor;
	private LinearLayout layout;

	public TimeAdapter(Context context, Cursor cursor) {
		this.context = context;
		this.cursor = cursor;
	}

	@Override
	public int getCount() {
		return cursor.getCount();
	}

	@Override
	public Object getItem(int position) {
		return cursor.getPosition();
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = LayoutInflater.from(context);
		layout = (LinearLayout) inflater.inflate(R.layout.tongjicell, null);
		TextView timetext = (TextView) layout.findViewById(R.id.text_tasktime);
		cursor.moveToPosition(position);
		String time = cursor.getString(cursor.getColumnIndex("tasktime"));
		timetext.setText(getTime()+"  此次积累"+time);
		return layout;
	}

	private String getTime() {
		SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
		Date curDate = new Date();
		String str = format.format(curDate);
		return str;
	}

	

}
